Namespacing everything to /UVa.
[and.git] / UVa / 10897 - Travelling Distance / 10897.2.cpp
blob447a6dac6ac2f8674087dacd0900f9ad85d1b71c
1 /*
2 Accepted
4 This is a cleaner version of 10897.cpp
5 */
6 using namespace std;
7 #include <iostream>
8 #include <cmath>
10 const long double rho = 6371.01;//km
11 const long double pi = 2*acos(0.0);
13 struct Vector{
14 //A geometrical vector
15 long double x, y, z;
16 long double dot(const Vector &t) const{
17 return x*t.x + y*t.y + z*t.z;
19 long double mag() const{
20 return sqrt(x*x + y*y + z*z);
24 struct point{
25 //A point in spherical coordinates
26 //assuming rho = 1
27 long double phi, theta;
28 Vector toVector() const{
29 Vector v;
30 v.x = sin(phi)*cos(theta);
31 v.y = sin(phi)*sin(theta);
32 v.z = cos(phi);
33 return v;
37 istream& operator >> (istream &in, point &p){
38 long double a, b, c, d, e, f;
39 char X, Y;
40 in >> a >> b >> c >> X >> d >> e >> f >> Y;
42 long double latitude, longitude;
43 latitude = (a + b/60.0 + c/3600.0);
44 longitude = (d + e/60.0 + f/3600.0);
46 p.phi = 90 + latitude * (X == 'N' ? -1.0 : 1.0);
47 p.theta = longitude * (Y == 'E' ? -1.0 : 1.0);
49 p.phi *= pi/180.0, p.theta *= pi/180.0; //"radianize"
50 return in;
53 long double dist(point &a, point &b){
54 //Convert shperical coordinates to rectangular coordinates...
55 Vector va = a.toVector(), vb = b.toVector();
56 //find angle between the two vectors...
57 long double angle = acos(va.dot(vb) / (va.mag()*vb.mag()) );
58 //calculate length of circle arc... (rho is the radius)
59 return rho * angle;
62 int main(){
63 int n;
64 for (scanf("%d", &n); n--; ){
65 point a, b;
66 cin >> a >> b;
67 cout.precision(2);
68 cout << fixed << dist(a, b) << endl;
70 return 0;